unit CtLstDlg;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, ExtCtrls,CrtTbls;

type
  TFormDBTblDef = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    LBTables: TListBox;
    BOk: TBitBtn;
    BCancel: TBitBtn;
    BAddDB: TBitBtn;
    BDelete: TBitBtn;
    BEdit: TBitBtn;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure FormShow(Sender: TObject);
    procedure BEditClick(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure BAddDBClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure addTablesFromXML(FileName:  tfilename);
    procedure LBTablesKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    FDatabaseName : string;
    FPassword : string;
    fdbDef: TDatabaseDef;
    procedure DeleteTable( idx: integer);
    procedure EnableControls;
    function GetDefinitions: TStrings;
    procedure SetDefinitions( d: TStrings );
    procedure Clear;
  public
    { Public declarations }
    function Execute: boolean;
    property Definitions: TStrings read GetDefinitions write SetDefinitions;
    property DatabaseName: string read FDatabaseName write FDatabaseName;
    property Password: string read FPassword write FPassword;
    property DbDef: TDatabaseDef read fdbDef write fdbdef;
//    property takeDefFromXml: Boolean;
  end;

implementation

{$R *.DFM}

uses
{  CrtTbls,} CtAdbDlg;

function TFormDBTblDef.GetDefinitions: TStrings;
begin
  result := LBTables.Items;
end;

procedure TFormDBTblDef.EnableControls;
begin
  BEdit.Enabled := LBTables.Items.Count>0;
  BDelete.Enabled := BEdit.Enabled;
  if LBTables.Visible then LBTables.ItemIndex := 0;
end;

procedure TFormDBTblDef.SetDefinitions( d: TStrings );
var
  i : integer;
begin
  Clear;
  LBTables.Items := d;
  with LBTables.Items do
    for i := 0 to Count-1 do
      Objects[i] := pointer(CloneTblDef(PFTableDef(Objects[i])));
  EnableControls;
end;

function TFormDBTblDef.Execute: boolean;
begin
  result := ShowModal=mrOk;
end;

procedure TFormDBTblDef.FormShow(Sender: TObject);
begin
  EnableControls;
end;

procedure TFormDBTblDef.Clear;
var
  i : integer;
  p : pointer;
begin
  with LBTables.Items do
    for i := 0 to Count-1 do
    begin
      p := Objects[i];
      DisposeTblDef(PFTableDef(p));
    end;
end;

procedure TFormDBTblDef.FormDestroy(Sender: TObject);
begin
  Clear;
end;

procedure TFormDBTblDef.BEditClick(Sender: TObject);
var
  v : string;
begin
  with LBTables do
  begin
    v := Items[ItemIndex];
    if not InputQuery('Edit table','Name[;Password]',v) then exit;
    Items[ItemIndex] := v;
  end;
end;

procedure TFormDBTblDef.DeleteTable( idx: integer);
var
  p : pointer;
begin
  if idx<0 then exit;
  with LBTables,Items do
  begin
    p := Objects[idx];
    DisposeTblDef(PFTableDef(p));
    Delete(idx);
  end;
end;

procedure TFormDBTblDef.BDeleteClick(Sender: TObject);
begin
  DeleteTable(LBTables.ItemIndex);
  EnableControls;
end;

procedure TFormDBTblDef.BAddDBClick(Sender: TObject);
var
  frmAddTbls : TFormAddDBTbls;
  i,p : integer;
  dbDef : TDatabaseDef;
begin
  frmAddTbls := TFormAddDBTbls.Create(Self);
  frmAddTbls.DatabaseName := FDatabaseName;

  if frmAddTbls.Execute then
  begin
    dbDef := TDatabaseDef.Create(Self);
    dbDef.DatabaseName := frmAddTbls.DatabaseName;
    dbDef.Password := FPassword;
    //Aca le pasa una lista de tablas y el objeto va a conseguir su definicion ...
    dbDef.Definitions := frmAddTbls.Tables;
    with dbDef.Definitions do
      for i := 0 to Count-1 do
      begin
        p := LBTables.Items.IndexOf(Strings[i]);
        if p>=0 then
        begin
          if MessageDlg('La tabla ya existe, la reemplaza ?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
          begin
            DeleteTable(p);
            p := -1;
          end;
        end;
        if p<0 then
          LBTables.Items.AddObject(Strings[i],pointer(CloneTblDef(PFTableDef(Objects[i]))));
      end;
    dbDef.Free;
  end;

  frmAddTbls.Release;
  EnableControls;
end;

procedure TFormDBTblDef.BitBtn1Click(Sender: TObject);
begin
  // TODO -cMM: TFormDBTblDef.BitBtn1Click default body inserted
  fdbDef.CreateXMLDatabaseDef;
end;

procedure TFormDBTblDef.BitBtn2Click(Sender: TObject);
var
  tod: TOpenDialog;
begin
  try
    tod := TOpenDialog.Create(nil);
    tod.Filter := 'Xml files (*.xml)|*.XML';
    tod.Execute;
    if tod.FileName = '' then
    begin
      MessageDlg('Elija un archivo xml para procesar por favor',  mtError, [mbOK], 0);
    end
    else
    begin
      Self.addTablesFromXML(tod.FileName);
    end

  finally
    freeandnil(tod);
  end;

end;
procedure TFormDBTblDef.LBTablesKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_DELETE: BDeleteClick(Self);
    VK_INSERT: BAddDBClick(Self);
    VK_RETURN: BEditClick(Self);
  end;
end;

procedure TFormDBTblDef.addTablesFromXML(FileName: tfilename);
var
  mydbdef : TDatabaseDef;
  i,p: Integer;
begin
    try
      mydbdef := TDatabaseDef.Create(nil);
      //Aca genera un database def con todas las definiciones famosas
      if mydbdef.ReadXMLDatabaseDef(filename) then
         Self.DbDef.DatabaseName:= mydbdef.DatabaseName;

      mydbdef.Password := FPassword;
      with mydbdef.Definitions do
        for i := 0 to Count-1 do
        begin
          p := LBTables.Items.IndexOf(Strings[i]);
          if p>=0 then
          begin
              DeleteTable(p);
              p := -1;
          end;
          if p<0 then
            LBTables.Items.AddObject(Strings[i],pointer(CloneTblDef(PFTableDef(Objects[i]))));
        end;
       finally
          mydbdef.Free;
     end;
  end;

end.
